<%@ page import="java.util.EnumSet" %>
<%@ page import="foo.bar.blog.core.security.RoleType" %>
<%@ page import="foo.bar.blog.core.domain.User" %>
<%@ page import="foo.bar.blog.core.security.PermissionType" %>
<%@include file="/WEB-INF/pages/commons/commons.jsp"%>
<%@include file="/WEB-INF/pages/commons/nocache.jsp"%>


<jsecurity:lacksPermission name="user:addUser:*">
    No "user:addUser:*" permission
</jsecurity:lacksPermission>

<jsecurity:hasPermission name="user:addUser:*">
<script type="text/javascript">
    $(document).ready(function(){
        $("#close").click(function() {
            $("#space").fadeOut();
        });
        $("#add").click(function() {
            $("#space").html('<fmt:message key="global.loading"/>');
            var form = this.form;
            var param = $(form).serializeArray();
            param.push({});
            var xhr = $.post(
                '<stripes:url beanclass="foo.bar.blog.web.action.administration.UserManagementActionBean" event="add"/>',
                param,
                function(data) {
                    $("#space").hide();
                    $("#space").html(data);
                    $("#space").fadeIn();
                    if (xhr.getResponseHeader('X-AjaxRequestStatus')) {
                        $("#table").html('<fmt:message key="global.loading"/>')
                        $.get(
                            '<stripes:url beanclass="foo.bar.blog.web.action.administration.UserManagementActionBean" event="userListing"/>',
                            function(data) {
                                $("#table").hide();
                                $("#table").html(data);
                                $("#table").fadeIn();
                            });
                        return false;
                    }
                }
            );
            return false;
        });
    });
</script>



<stripes:messages/>
<stripes:errors globalErrorsOnly="true" />


<h2>Add User</h2>
<stripes:form method="POST" beanclass="foo.bar.blog.web.action.administration.UserManagementActionBean">
        <p>
            <a id="close" href=""><fmt:message key="viewUserDetails.jsp.close"/></a>
        </p>
        <p>
            <stripes:errors/>

            <table width="100%">
            <tr>
                <td><stripes:label for="user.username"/>*</td>
                <td><stripes:text name="user.username" /></td>
            </tr>
            <tr>
                <td><stripes:label for="user.email"/>*</td>
                <td><stripes:text name="user.email"/></td>
            </tr>
            <tr>
                <td><stripes:label for="user.password"/>*</td>
                <td><stripes:password name="user.password"/></td>
            </tr>
            <tr>
                <td><stripes:label for="confirmPassword"/>*</td>
                <td><stripes:password name="confirmPassword"/></td>
            </tr>
            <tr>
                <td><stripes:label for="user.expiryDate"/></td>
                <td><stripes:text name="user.expiryDate"/></td>
            </tr>
            <tr>
                <td><stripes:label for="user.locked"/></td>
                <td><stripes:checkbox name="user.locked" value="true"/></td>
            </tr>
            <tr>
                <td><stripes:label for="user.disabled"/></td>
                <td><stripes:checkbox name="user.disabled" value="true"/></td>
            </tr>
            </table>


            <h3><label><fmt:message key="foo.bar.blog.web.action.administration.UserManagementActionBean.user.roles"/></label></h3>
            <table width="100%">
                <thead>
                <tr>
                    <th><fmt:message key="editUserDetails.jsp.rolesTable.roleNameColumn"/></th>
                    <th><fmt:message key="editUserDetails.jsp.rolesTable.roleDescriptionColumn"/></th>
                    <th><fmt:message key="editUserDetails.jsp.rolesTable.roleEnabledColumn"/></th>
                </tr>
                </thead>
                <tbody>
                <c_rt:set var="allRoles" value="<%=EnumSet.allOf(RoleType.class)%>" />
                <c:forEach var="role" items="${allRoles}">
                    <c:set var="fmtRoleName" value="global.roles.name.${role}"/>
                    <c:set var="fmtRoleDescription" value="global.roles.description.${role}"/>
                    <tr>
                        <td>
                            <fmt:message key="${fmtRoleName}" />
                        </td>
                        <td>
                            <fmt:message key="${fmtRoleDescription}" />
                        </td>
                        <td>
                            <c:set var="currentUser" value="${actionBean.user}" />
                            <c:set var="currentRoleType" value="${role}"/>
                            <c_rt:set var="containsRole" value='<%=((User)pageContext.getAttribute("currentUser")).hasRole(((RoleType)pageContext.getAttribute("currentRoleType")))%>' />
                            <c_rt:set var="currentRole" value='<%=((User)pageContext.getAttribute("currentUser")).getRole(((RoleType)pageContext.getAttribute("currentRoleType")))%>' />
                            <input type="checkbox" name="roles" value='<c:out value="${currentRoleType}"/>'
                                <c:choose>
                                <c:when test="${containsRole}">
                                    checked="true"
                                </c:when>
                                </c:choose>
                                    />
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
            <p/>
            <p/>

            <h3><label><fmt:message key="foo.bar.blog.web.action.administration.UserManagementActionBean.user.permissions"/></label></h3>
            <table width="100%">
                <thead>
                <tr>
                    <th><fmt:message key="editUserDetails.jsp.permissionsTable.permissionNameColumn"/></th>
                    <th><fmt:message key="editUserDetails.jsp.permissionsTable.permissionDescriptionColumn"/></th>
                    <th><fmt:message key="editUserDetails.jsp.permissionsTable.permissionEnabledColumn"/></th>
                </tr>
                </thead>
                <tbody>
                <c_rt:set var="allPermissions" value="<%=EnumSet.allOf(PermissionType.class)%>" />
                <c:forEach var="permission" items="${allPermissions}">
                    <c:set var="fmtPermissionName" value="global.permissions.name.${permission}"/>
                    <c:set var="fmtPermissionDescription" value="global.permissions.description.${permission}" />
                    <tr>
                        <td>
                            <fmt:message key="${fmtPermissionName}"/>
                        </td>
                        <td>
                            <fmt:message key="${fmtPermissionDescription}"/>
                        </td>
                        <td>
                            <c:set var="currentUser" value="${actionBean.user}"/>
                            <c:set var="currentPermissionType" value="${permission}"/>
                            <c_rt:set var="containsPermission" value='<%=((User)pageContext.getAttribute("currentUser")).hasPermission(((PermissionType)pageContext.getAttribute("currentPermissionType")))%>' />
                            <c_rt:set var="currentPermission" value='<%=((User)pageContext.getAttribute("currentUser")).getPermission(((PermissionType)pageContext.getAttribute("currentPermissionType")))%>' />
                            <input type="checkbox" name="permissions" value='<c:out value="${currentPermissionType}"/>'
                                   <c:choose>
                                   <c:when test="${containsPermission}">
                                       checked="true"
                                   </c:when>
                                   </c:choose>
                                    />
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
            <p/>
            <stripes:submit id="add" name="add"><fmt:message key="addUser.jsp.submit" /></stripes:submit>
        </p>
</stripes:form>
<p/>
<p/>
<p/>
</jsecurity:hasPermission>

